package com.luying.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luying.constants.RoleTypeEnum;
import com.luying.dao.TeacherJobInformationDao;
import com.luying.pojo.*;
import com.luying.service.TeacherJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class TeacherJobServiceImpl implements TeacherJobService{
    @Autowired
    private TeacherJobInformationDao teacherJobInformationDao;
    @Override
    public int add(TeacherJobInformation job) throws Exception {
        try {
            if(job.getId()!=null){
                Example example = new Example(TeacherJobInformation.class);
                example.createCriteria().andEqualTo("id",job.getId());
                teacherJobInformationDao.deleteByExample(example);
            }
            return teacherJobInformationDao.insertSelective(job);
        } catch (Exception e) {
            throw new Exception("不能重复设置同一名教师负责同一个班级的同一门科目");
        }
    }

    @Override
    public PageResult query(PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<JobList> list =  teacherJobInformationDao.selectPage(pageQuery);
        PageInfo<JobList> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;
    }

    @Override
    public List<BaseDown> teacherList() {
        List<BaseDown> list = teacherJobInformationDao.teacherList(RoleTypeEnum.TEACHER.getName());
        return list;
    }

    @Override
    public List<BaseDown> classList() {
        List<BaseDown> list = teacherJobInformationDao.classList();
        return list;
    }

    @Override
    public List<BaseDown> subjectList(Integer id) {
        List<BaseDown> list = teacherJobInformationDao.subjectList(id);
        return list;
    }
}
